//
//  MCKefuViewController.m
//  TLYL
//
//  Created by miaocai on 2017/8/8.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCKefuViewController.h"
#import "MCKefuModel.h"
#import "MCMailNavigationController.h"

@interface MCKefuViewController ()<UIWebViewDelegate>

@property (nonatomic,strong) MCKefuModel *model;
@property (nonatomic,weak) UIWebView *web;

@end

@implementation MCKefuViewController

#pragma mark -- life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationItem.title = @"联系客服";
    self.automaticallyAdjustsScrollViewInsets = NO;
    CGFloat h = kStatusBarAndNavigationBarHeight;
    if (IS_iPhoneX) {
        h= h - 35;
    }
  // UI
    UIWebView *web = [[UIWebView alloc] init];
    web.delegate = self;
    [self.view addSubview:web];
    self.web = web;
    
    if ([tabbarThdSelected isEqualToString:@"tabbarThdSelected"]) {
        if (@available(iOS 11.0, *)) {
            web.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        web.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT -h);
    } else {
        if (self.tabBarController) {
            web.frame = CGRectMake(0, 0, G_SCREENWIDTH, G_SCREENHEIGHT - h);
        } else {
            web.frame = CGRectMake(0, kStatusBarAndNavigationBarHeight, G_SCREENWIDTH, G_SCREENHEIGHT - h);
        }
        
        
    }


    // Requst

    MCKefuModel *model = [[MCKefuModel alloc] init];
    self.model = model;
    [model refreashDataAndShow];
    
    
    model.callBackSuccessBlock = ^(id manager) {
        if (![[manager allKeys] containsObject:@"ServiceUrl"]) {
            [SVProgressHUD showInfoWithStatus:@"客服不可用"];
            return;
        }
        NSString * ServiceUrl = [NSString stringWithFormat:@"%@",manager[@"ServiceUrl"]];
        if ([ServiceUrl isEqualToString:@""]||[ServiceUrl containsString:@"null"]) {
            [SVProgressHUD showInfoWithStatus:@"客服不可用"];
            return;
        } else {
            [self.web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:manager[@"ServiceUrl"]]]];
        }
    };
    
    model.callBackFailedBlock = ^(id manager, NSString *errorCode) {
       [SVProgressHUD showInfoWithStatus:@"客服不可用"];
    };


}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [SVProgressHUD dismiss];
}

#pragma mark -- webView delegate

- (void)webViewDidStartLoad:(UIWebView *)webView{
    [SVProgressHUD show];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    [SVProgressHUD dismiss];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    if (error.code == -1001 ||error.code == 4) {

        [SVProgressHUD showInfoWithStatus:@"页面加载超时！"];
    } else {
        [SVProgressHUD showInfoWithStatus:@"页面无法加载！"];

    }
}

#pragma mark -- 返回按钮点击
- (void)btnClick{
    [self dismissViewControllerAnimated:YES completion:nil];
    }


@end
